package com.yang.controller;


import com.yang.anno.Log;
import com.yang.pojo.Emp;
import com.yang.pojo.PageBean;
import com.yang.pojo.Result;
import com.yang.service.EmpService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RequestMapping("/emps")
@RestController
public class EmpController {

    @Resource(name = "empServiceImpl")
    private EmpService empService;

    /**
     * 请求参数: 页码 每页展示记录数
     * 响应结果: 总记录数 结果列表
     * 注解 @RequestParam(defaultValue = "1"): 设置请求参数默认值
     * @param page
     * @param pageSize
     * @return
     */

    @Log
    @GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name, Integer gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        log.info("分页查询参数 页码,查询记录数,名字, 性别, 开始时间, 结束时间:{},{}，{}，{} {} {}", page, pageSize, name, gender, begin, end);
        PageBean pageBean = empService.page(page, pageSize, name, gender, begin, end);
        return Result.success(pageBean);

    }

    @Log
    @DeleteMapping("/{ids}")
    public Result deleteEmp(@PathVariable List<Integer> ids){
        log.info("批量删除操作,ids:{}", ids);
        empService.delete(ids);
        return Result.success();
    }

    @Log
    @PostMapping
    public Result addEmp(@RequestBody Emp emp){
        log.info("新增员工,emp:{}", emp);
        empService.add(emp);
        return Result.success();
    }

    @Log
    @GetMapping("/{id}")
    public Result getByIdEmp(@PathVariable Integer id){
        log.info("根据id查询员工信息 id{}:", id);
        Emp emp = empService.getById(id);
        return Result.success(emp);
    }

    @Log
    @PutMapping
    public Result updateEmp(@RequestBody Emp emp){
        log.info("更新员工信息 :{}", emp);
        empService.update(emp);
        return Result.success();
    }

}
